//
//  MixTemplateView.m
//  ResourceLoader
//
//  Created by winston on 13-4-11.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "MixTemplateView.h"

@implementation MixTemplateView

-(void)renderTemplate
{
    self.type = MixTemplateType;
//    self.playPath = [NSString stringWithFormat:@"%@/%@",[[ResourceLoader getInstance]getModulePathByKey:self.moduleKey withAutoID:self.autoID],self.bigImageName];
    self.playPath = [NSString stringWithFormat:@"%@/%@",self.rootPath,self.bigImageName];
    
    if(imageView==nil)
    {
        //因为背景图有2个像素的阴影
        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(2,1,self.frame.size.width-5,self.frame.size.height-6)];
        [self addSubview:imageView];
        [imageView release];
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    
//    NSString * imageFile = [NSString stringWithFormat:@"%@/%@",[[ResourceLoader getInstance]getModulePathByKey:self.moduleKey withAutoID:self.autoID],self.middleImageName];
    NSString * imageFile = [NSString stringWithFormat:@"%@/%@",self.rootPath,self.middleImageName];
    UIImage * image = [[UIImage alloc]initWithContentsOfFile:imageFile];
    if(image)
    {
        [imageView setImage:image];
        [image release];
    }
    
    
    if(titlebackground==nil)
    {
        titlebackground = [[UIView alloc] initWithFrame:CGRectMake(2, self.frame.size.height-30, self.frame.size.width-5, 25)];
        [titlebackground setBackgroundColor:[UIColor colorWithRed:0.29 green:0.29 blue:0.29 alpha:1.0]];
        [self addSubview:titlebackground];
        [titlebackground release];
        
        
    }
    
    if (shadowView == nil) {
        shadowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AllAuto_Appearance_Image_Shadow"]];
        [self addSubview:shadowView];
        [shadowView release];
        shadowView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y + imageView.frame.size.height, imageView.frame.size.width, 0);
        shadowView.alpha = 0;
    }
    
    
    
    if(titleLabel==nil)
    {
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, titlebackground.frame.size.width-10, titlebackground.frame.size.height)];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [titlebackground addSubview:titleLabel];
        [titleLabel release];
        [titleLabel setText:self.textString];
        titleLabel.numberOfLines = 0;
    }
    
    
    if (self.contentString != nil) {
        
        if(contentbackground==nil)
        {
            contentbackground = [[UIView alloc] initWithFrame:CGRectZero];
            [contentbackground setBackgroundColor:[UIColor whiteColor]];
            [self addSubview:contentbackground];
            [contentbackground release];
            contentbackground.clipsToBounds = YES;
        }
        
        if(contentTextView == nil)
        {
            contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, contentbackground.frame.size.width, contentbackground.frame.size.height)];
            [contentbackground addSubview:contentTextView];
            [contentTextView release];
            contentTextView.editable = NO;
            contentTextView.backgroundColor = [UIColor clearColor];
        }
        
        contentTextView.text = self.contentString;
    }

}

-(void)TransmutationSmall
{

    imageView.frame = CGRectMake(2,1,self.frame.size.width-5,self.frame.size.height-6);
    titlebackground.frame = CGRectMake(2, self.frame.size.height-30, self.frame.size.width-5, 25);
    titleLabel.frame = CGRectMake(5, 0, titlebackground.frame.size.width-10, titlebackground.frame.size.height);
    
    if (contentbackground) {
        contentbackground.frame = CGRectZero;
        contentTextView.frame = CGRectMake(0, 0, contentbackground.frame.size.width, contentbackground.frame.size.height);
    }
    
    shadowView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y + imageView.frame.size.height, imageView.frame.size.width, 0);
    shadowView.alpha = 0;
    
    [self bringSubviewToFront:titlebackground];
    backimageViewSmall.hidden = NO;
    backimageViewMiddle.hidden = YES;
}

-(void)TransmutationMiddle
{
    imageView.frame = CGRectMake(10,5,656,493);
    
    int titleheight = 0;
    int titlewidth  = 0;
    
    if (contentbackground) {
        titlebackground.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y+imageView.frame.size.height, 180, 81);
        
        titlewidth  = titlebackground.frame.size.width-8;
        titleheight = [self caculateTitleHeight:self.textString FontSize:13 Width:titlewidth];
        if (titleheight>titlebackground.frame.size.height-16) {
            titleheight = titlebackground.frame.size.height-16;
        }
        titleLabel.frame = CGRectMake(4, 8, titlewidth,titleheight);
        
        contentbackground.frame = CGRectMake(titlebackground.frame.origin.x+titlebackground.frame.size.width, titlebackground.frame.origin.y, TemplateWidthMiddle - (titlebackground.frame.origin.x+titlebackground.frame.size.width+10), titlebackground.frame.size.height);
        
        contentTextView.frame = CGRectMake(4, 8, contentbackground.frame.size.width-8, contentbackground.frame.size.height-16);
    }
    else
    {
        
        titlebackground.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y+imageView.frame.size.height, imageView.frame.size.width, 81);//111
        
        titlewidth  = titlebackground.frame.size.width-8;
        titleheight = [self caculateTitleHeight:self.textString FontSize:13 Width:titlewidth];
        if (titleheight>titlebackground.frame.size.height-16) {
            titleheight = titlebackground.frame.size.height-16;
        }
        titleLabel.frame = CGRectMake(4, 8, titlewidth,titleheight);
    }
    
    shadowView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y + imageView.frame.size.height, imageView.frame.size.width, 20);
    shadowView.alpha = 1;

    backimageViewSmall.hidden = YES;
    backimageViewMiddle.hidden = NO;
    [self bringSubviewToFront:imageView];
    [self bringSubviewToFront:shadowView];
}

@end
